import axios from "axios"


let config = {}

const { VUE_APP_PUBLIC_PATH } = process.env

const setConfig = (cfg) => {
    config = Object.assign(config, cfg)
}

const getConfig = (key) => {
    if (typeof key === 'string') {
        const arr = key.split(".")
        if (arr && arr.length) {
            let data = config
            data.forEach(v => {
                if (data && typeof data[v] !== "undefined") {
                    data = data[v];
                } else {
                    data = null;
                }
            })
            return data;
        }
    }
    return config;
}

// 获取项目动态全局配置
export const getServerConfig = async (app) => {
    app.prototype.$config = getConfig();
    return axios({
        baseURL: "",
        method: "get",
        url: `${VUE_APP_PUBLIC_PATH}serverConfig.json`
    }).then(({ data: config }) => {
        let $config = app.prototype.$config;
        // 自动注入项目配置
        if (app && $config && typeof config === "object") {
            $config = Object.assign($config, config);
            app.prototype.$config = $config;
            //设置全局配置
            setConfig($config);
            return $config;
        }
        // 设置全局baseURL
        app.prototype.$baseUrl = $config.baseURL;
    }).catch(() => {
        throw "请在public文件夹下添加serverConfig.json配置文件";
    })
};

export { getConfig, setConfig };